<template>
  <div>
    <div>
      <el-upload
          class="upload-demo"
          action="http://localhost:8100/service/process/import"
          multiple
          :limit="3"
          accept=".xls,.xlsx"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <el-form :inline="true" :model="formInline" class="demo-form-inline" >
        <el-form-item label="任务名称" >
          <el-input v-model="formInline.quest" placeholder="任务名称"></el-input>
        </el-form-item>
        <el-form-item label="加工状态">
        <el-select v-model="formInline.status" placeholder="加工状态" clearable>
          <el-option  label="未加工" value="0"></el-option>
          <el-option  label="加工中" value="1"></el-option>
          <el-option  label="已加工" value="2"></el-option>
        </el-select>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list">查询</el-button>
          <el-button left type="primary" @click="openAdd">添加</el-button>
          <el-button type="primary" @click="excel">导出</el-button>
          <el-button @click="getdp()" type="primary">大屏展示</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="processId"
            label="加工id"
            width="120">
        </el-table-column>
        <el-table-column
            prop="cropName"
            label="农作物名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="cargoName"
            label="仓库名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="quest"
            label="任务名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="questNum"
            label="任务数量"
            width="120">
        </el-table-column>
        <el-table-column
            prop="statu"
            label="加工状态"
            width="120">
          <template v-slot="s">
            <el-tag v-if="s.row.statu==1" type="info">未加工</el-tag>
            <el-tag v-if="s.row.statu==2" type="warning">加工中</el-tag>
            <el-tag v-if="s.row.statu==3" type="success">已加工</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
            width="200">
        </el-table-column>
        <el-table-column
            prop="cityName"
            label="省/市/区"
            width="200">
          <template v-slot="r">
            {{r.row.provinceName}}  - {{r.row.cityName}} - {{r.row.districtName}}
          </template>
        </el-table-column>
        <el-table-column
            prop="pic"
            label="图片"
            width="200">
          <template v-slot="s">
            <el-image
                style="width: 100px;height: 100px"
                :src="s.row.pic"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
            prop="remark"
            label="备注"
            width="120">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="del(scope.row.processId)" type="text" size="small">删除</el-button>
            <el-button v-if="scope.row.statu==3"  @click="getqs(scope.row)" type="text" size="small">运输</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @current-change="goPage"
          :page-size="5"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>

      <el-dialog title="加工表添加" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="ruleForm">
          <el-form-item label="任务名称" :label-width="formLabelWidth" prop="quest">
            <el-input v-model="form.quest" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="任务数量" :label-width="formLabelWidth">
            <el-input v-model="form.questNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="农作物名称" :label-width="formLabelWidth">
            <el-select v-model="form.cropNum" placeholder="请选择农作物">
              <el-option
                  v-for="item in getcrops"
                  :key="item.cropId"
                  :label="item.name"
                  :value="item.cropId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="仓库名称" :label-width="formLabelWidth">
            <el-select v-model="form.cargoNum" placeholder="请选择仓库">
              <el-option
                  v-for="item in getcargo"
                  :key="item.cargoId"
                  :label="item.cargoName"
                  :value="item.cargoId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="省/市/区" :label-width="formLabelWidth">
            <el-cascader
                v-model="form.cascaderValue"
                :options="options"
                :props="props"
                @change="getchange"
                clearable>
            </el-cascader>
          </el-form-item>
          <el-form-item label="图片上传" :label-width="formLabelWidth">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8100/service/process/addPic"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="加工状态" :label-width="formLabelWidth">
            <el-radio v-model="form.statu" label="1">未加工</el-radio>
            <el-radio v-model="form.statu" label="2">加工中</el-radio>
            <el-radio v-model="form.statu" label="3">已加工</el-radio>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input v-model="form.remark" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="加工表修改" :visible.sync="dialogFormVisible1">
        <el-form :model="form1">
          <el-form-item label="任务名称" :label-width="formLabelWidth1">
            <el-input v-model="form1.quest" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="任务数量" :label-width="formLabelWidth1">
            <el-input v-model="form1.questNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="农作物名称" :label-width="formLabelWidth1">
            <el-select v-model="form1.cropNum" placeholder="请选择农作物">
              <el-option
                  v-for="item in getcrops"
                  :key="item.cropId"
                  :label="item.name"
                  :value="item.cropId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="仓库名称" :label-width="formLabelWidth1">
            <el-select v-model="form1.cargoNum" placeholder="请选择仓库">
              <el-option
                  v-for="item in getcargo"
                  :key="item.cargoId"
                  :label="item.cargoName"
                  :value="item.cargoId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="省/市/区" :label-width="formLabelWidth1">
            <el-cascader
                v-model="areaIds"
                :options="options"
                :props="props"
                @change="getchange1"
                clearable>
            </el-cascader>
          </el-form-item>
          <el-form-item label="图片上传" :label-width="formLabelWidth1">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8100/service/process/addPic"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="加工状态" :label-width="formLabelWidth1">
            <el-radio v-model="form1.statu" label="1">未加工</el-radio>
            <el-radio v-model="form1.statu" label="2">加工中</el-radio>
            <el-radio v-model="form1.statu" label="3">已加工</el-radio>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth1">
            <el-input v-model="form1.remark" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="签收" width="1200px" append-to-body :visible.sync="dialogFormVisible2">
<!--              <el-col :span="12">零售商：-->
<!--                <el-select v-model="form2.retailId" placeholder="请选择零售商">-->
<!--                  <el-option-->
<!--                      v-for="item in getretail"-->
<!--                      :key="item.shopId"-->
<!--                      :label="item.productName"-->
<!--                      :value="item.shopId">-->
<!--                  </el-option>-->
<!--                </el-select>-->
<!--              </el-col>-->
        <el-table :data="gridData">
          <el-table-column property="tid" label="运输编号" width="100"></el-table-column>
          <el-table-column property="quest" label="任务名称" width="100"></el-table-column>
          <el-table-column property="questNum" label="任务数量" width="100"></el-table-column>
          <el-table-column property="cropName" label="农作物名称" width="120"></el-table-column>
          <el-table-column property="cargoName" label="仓库名称" width="120"></el-table-column>
          <el-table-column property="createTime" label="创建时间" width="180"></el-table-column>
          <el-table-column property="drivers" label="司机"></el-table-column>
          <el-table-column property="status" label="运输状态">
            <template v-slot="r">
              <el-tag type="success" v-if="r.row.status==1||r.row.status==2 ">
                {{r.row.status==1?'未运输':''}}
                {{r.row.status==2?'运输中':''}}
                {{r.row.status==3?'运输完成':''}}
              </el-tag>
              <el-tag type="danger" v-if="r.row.status==3">
                {{r.row.status==3?'运输完成':''}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column property="qstatus" label="是否签收">
            <template v-slot="r">
              <el-tag type="success"  v-if="r.row.qstatus==1||r.row.qstatus==2 ">
                {{r.row.qstatus==1?'代签收':''}}
                {{r.row.qstatus==2?'已签收':''}}
              </el-tag>
              <el-tag type="danger" v-if="r.row.qstatus==3">
                {{r.row.qstatus==3?'拒签':''}}
              </el-tag>
            </template>
          </el-table-column>
<!--          <el-table-column property="productName" label="零售商" width="180">-->
<!--            <el-form :model="form2">-->
<!--              <el-select v-model="form2.retailId" placeholder="请选择零售商">-->
<!--                <el-option-->
<!--                    v-for="item in getretail"-->
<!--                    :key="item.shopId"-->
<!--                    :label="item.productName"-->
<!--                    :value="item.shopId">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form>-->
<!--          </el-table-column>-->
          <el-table-column
              fixed="right"
              label="操作"
              width="200">
            <template slot-scope="scope">
              <el-button type="text"  @click="getqx(scope.row.tid)" icon="el-icon-search" style="size: 10px">签收</el-button>
              <el-button type="text"  @click="getjuqian(scope.row.tid)" icon="el-icon-search" style="size: 10px">拒签</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-dialog>
    </div>


    <el-button @click="initChart" type="primary">展开折线图</el-button>
    <div>
      <div id="main" style="height: 300px;width: 500px"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "SrcProcessView",
  data() {
    return {
      gridData:[],
      getcrops:[],
      getcargo:[],
      getretail:[],
      formInline:{},
      tableData:[],
      pageNum:1,
      total:0,
      props: {
        value: 'id',
        label: 'name',
        children: 'children'
      },
      options: [],
      form:{
        cascaderValue:[],
      },
      form1:{},
      form2:{},
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2:false,
      formLabelWidth: '120px',
      formLabelWidth1: '120px',
      rules: {
        quest: [
          {required: true, message: '请输入任务名称', trigger: 'blur'},
        ],
        cropName: [
          {required: true, message: '请输入材料名称', trigger: 'blur'},
        ],
      },
      areas:[],
      areaIds:[0,0,0],
      imageUrl:'',
      zxt:[],
      x:[],
      y:[],

    }
  }, methods: {
    getqx(id) {

      this.axios.post("/service/logistics/updateqs?id="+id).then(res=>{
        if(res.data.code==200)
        {
          this.$message.success(res.data.msg);
          this.dialogFormVisible2=false
          this.getlist();
        }else
        {
          this.$message.error(res.data.msg);
        }
      })
    },
    getjuqian(id)
    {
      console.log(id);
      this.axios.post("/service/logistics/getjq?id="+id).then(res=>{
        if(res.data.code==200)
        {
          this.$message.success(res.data.msg);
          this.dialogFormVisible2=false
          this.getlist();
        }else
        {
          this.$message.error(res.data.msg);
        }
      })

    },
    getqs(scope){
      this.axios.post("/service/process/qs?processId="+scope.processId).then(res=>{
        this.gridData=res.data.data;
      })
      this.dialogFormVisible2=true;
    },

    getdp()
    {
      this.$router.push("/da")
    },
    getchange1(id)
    {
      console.log(id);
      if (id.length > 0) {
        this.form1.provinceId = id[0];
        this.form1.cityId = id[1];
        this.form1.areaId = id[2];
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.pic=res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    excel(){
      window.location.href=this.axios.defaults.baseURL+"service/process/export"
    },
    gettype() {
      this.axios.post("/service/process/gettype").then(res => {
        this.options = res.data.data;
      });
    },
    getchange(id) {
      console.log(id);
      if (id.length > 0) {
        this.form.provinceId = id[0];
        this.form.cityId = id[1];
        this.form.areaId = id[2];
      } else {
        this.form.provinceId = '';
      }
    },
    list(){
      this.axios.post("/service/process/list?pageNum="+this.pageNum,this.formInline).then(res=>{
        this.tableData=res.data.data.list;
        this.total=res.data.data.total;
      })
    },
    goPage(pageNum){
      this.pageNum=pageNum;
      this.list();
    },
    // initAreas() {
    //   this.axios.get("/service/process/listTree").then(res => {
    //     this.areas=res.data;
    //   });
    // },
    openAdd(){
      this.imageUrl='';
      this.areaIds=[];
      this.gettype();
      this.form = {statu:'1'};
      this.form.processId = "";
      this.dialogFormVisible=true;
    },
    add(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("/service/process/add",this.form).then(res=>{
            if (res.data.code==200){
              this.dialogFormVisible=false;
              this.$message.success(res.data.msg);
              this.list();
            }else{
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    openUpdate(row){
      this.areaIds=[];
       this.form1=row;
       this.imageUrl=row.pic;
      // console.log(row.processId)
      // this.form1.cropNum=row.cropName;
      // this.form1.cargoNum=row.cargoName;
      // console.log(this.form1.cargoName)

      if(row.provinceId){
        this.areaIds.push(row.provinceId,row.cityId,row.areaId);
      }
      console.log(this.areaIds);
      this.axios.post("/service/process/search?id="+row.processId).then(res=>{
        this.form1=res.data.data;
        this.form1.cropName=res.data.data.cropName;
        this.form1.cargoName=res.data.data.cargoName;
        this.form1.productName=res.data.data.productName;
      })
      this.dialogFormVisible1=true;
    },
    update(){
      this.axios.post("/service/process/update",this.form1).then(res=>{
        if (res.data.code==200){
          this.dialogFormVisible1=false;
          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    del(processId){
      this.$confirm('此操作将永久删除数据，是否继续？','温馨提示',{
        confirmButtonText:'确定',
        cancelButtonClass:'取消',
        type:'warning'
      }).then(()=>{
        this.axios.post("/service/process/del?processId="+processId).then(res=>{
          if (res.data.code==200){
            this.list();
          }else{
            this.$message.error(res.data.msg);
          }
        })
        this.$message({
          type:'success',
          message:'删除成功',
        })
      }).catch(()=>{
        this.$message({
          type:'info',
          message:'已取消删除',
        })
      })

    },
    getCropList(){
      this.axios.post("/service/process/getCropList").then(res=>{
        this.getcrops=res.data.data;
      })
    },
    getCargoList(){
      this.axios.post("/service/process/getCargoList").then(res=>{
        this.getcargo=res.data.data;
      })
    },
    getRetailList(){
      this.axios.post("/service/process/getRetailList").then(res=>{
        this.getretail=res.data.data;
      })
    },
    initChart(){
      this.axios.post("/service/process/getTu").then(res=>{
        this.zxt=res.data.data;
        let quest=[];
        let questNum=[];
        this.zxt.forEach(item=>{
          quest.push(item.xzhou);
          questNum.push(item.yzhou);

          console.log(this.x);
          console.log(this.y);
        });

        this.x=quest;
        this.y=questNum;
        let echarts = require('echarts');
        var chartDom = document.getElementById('main');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
          xAxis: {
            type: 'category',
            data: this.x
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: this.y,
              type: 'line'
            }
          ]
        };
        console.log(this.x);
        console.log(this.y);

        option && myChart.setOption(option);
      })

    }
  }, created() {
    this.list();
    this.getCropList();
    this.gettype();
    this.getCargoList();
    this.getRetailList();
    //this.initAreas();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>